package com.iflytek.uoamp.notify.mail.format;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.iflytek.uoamp.common.notify.obj.BaseInfo;
import com.iflytek.uoamp.common.notify.obj.BizInfo;
import com.iflytek.uoamp.common.notify.obj.Content;
import com.iflytek.uoamp.common.notify.obj.MonitorInfo;
import com.iflytek.uoamp.common.notify.obj.Notification;
import com.iflytek.uoamp.common.notify.util.NotifyUtils;

public class FormatBizMail extends FixedMailFormatter {
	
    private static final String BIZTEMPLATE = getTemplate("biz_template.html");
    
    private int rowCounts = 0;
    
    @Override
    public String pretty(Notification notification, List<?> contents) {
        StringBuilder content = new StringBuilder(getTitle(contents) + "@@" + BIZTEMPLATE);
        StringBuilder data = new StringBuilder();
        
        String dataTemplate = getDataTemplate(BIZTEMPLATE);
        String rowTemplate = getRowTemplate(BIZTEMPLATE);
        
        for(Object item : contents) {
            String block = fillTemplate((Content)item, rowTemplate, dataTemplate);
            data.append(block);
        }
        replace(data, "${rows}", Integer.toString(rowCounts));
        fillAllData(content, data.toString());


        //wlhu2, 添加组件联系人信息
        fillPrincipalData(content, notification);
        return content.toString();
    }
    
    private String fillTemplate(Content content, String rowTemplate, String dataTemplate) {
        StringBuilder result = new StringBuilder();
        StringBuilder row = null;
        
        if (rowCounts == 0) {
            row = new StringBuilder(rowTemplate);
        } else {
            row = new StringBuilder(dataTemplate);
        }
        MonitorInfo monitorInfo = content.getMonitorInfo();
        if(monitorInfo != null) {
        	BaseInfo baseInfo = monitorInfo.getBaseInfo();
        	BizInfo bizInfo = monitorInfo.getBizInfo();
//        	CompInfo compInfo = monitorInfo.getCompInfo();
        	
	        replace(row, "${monitortime}", content.getMonitorTime());
	        replace(row, "${hostip}", baseInfo == null ? "" : StringUtils.isEmpty(baseInfo.getIP()) || "null".equals(baseInfo.getIP()) ?"":baseInfo.getIP());
	        replace(row, "${bizname}", bizInfo == null ? "" : StringUtils.isEmpty(bizInfo.getBizName()) || "null".equals(bizInfo.getBizName())?"":bizInfo.getBizName());
//	        replace(row, "${cmdname}", bizInfo == null ? "" : StringUtils.isEmpty(bizInfo.getCmdName()) || "null".equals(bizInfo.getCmdName())?"":bizInfo.getCmdName());
//	        replace(row, "${componentname}", compInfo == null ? "" : StringUtils.isEmpty(compInfo.getComponentName()) || "null".equals(compInfo.getComponentName())?"":compInfo.getComponentName());
	        replace(row, "${monitorptname}", bizInfo == null ? "" : StringUtils.isEmpty(bizInfo.getMonitorptName()) || "null".equals(bizInfo.getMonitorptName())?"":bizInfo.getMonitorptName());
	        replace(row, "${mailstatedes}", NotifyUtils.getAttributeValue(content.getOutput(), "mailstatedes"));
	        if (rowCounts == 0) {
	            replace(row, "${remark}", remark);
	        }
	        result.append(row);
	        ++rowCounts;
        }
        
        return result.toString();
    }
}
